/*  
 *  ------ [DM_08a] - complete example send  ------
 *  
 *  Explanation: This is a complete example for XBee Digimesh.
 *  This example shows how to send a packet using unicast 64-bit
 *  destination address. After sending the packet, Waspmote waits
 *  for the response of the receiver and prints all available 
 *  information  
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <WaspXBeeDM.h>
#include <WaspFrame.h>

///////  Input parameters  //////////
char* DESTINATION="0013A200406C16CE";
////////////////////////////////////

// pointer to the packet to send
packetXBee* packet;

// variable to store time stamp
long previous=0;


void setup()
{
  // init USB port
  USB.ON();
  USB.println(F("DM_08a example"));

  // init XBee module
  xbeeDM.ON(); 

}


void loop()
{
  //////////////////////////
  // 1. create frame
  //////////////////////////  
  
  // 1.1. create new frame
  frame.createFrame(ASCII, "WASPMOTE_08a");  
  
  // 1.2. add frame fields
  frame.addSensor(SENSOR_STR, "Complete example message"); 
  frame.addSensor(SENSOR_RAM, freeMemory()); 
  
  
  //////////////////////////
  // 2. send packet
  //////////////////////////  

  // 2.1. set packet to send:
  packet=(packetXBee*) calloc(1,sizeof(packetXBee)); // memory allocation
  packet->mode=UNICAST; // set Broadcast mode

  // 2.2. set Destination parameters
  xbeeDM.setDestinationParams(packet, DESTINATION, frame.buffer, frame.length);

  // 2.3. send data
  xbeeDM.sendXBee(packet);

  // 2.4. check TX flag
  if( xbeeDM.error_TX == 0 ) 
  {   
    USB.println(F("ok")); 
  }
  else 
  {
    USB.println(F("error")); 
  }
  
  // 2.5. free memory
  free(packet);
  packet=NULL; 


  //////////////////////////
  // 3. receive answer
  //////////////////////////  

  // wait the answer for 5 seconds
  previous=millis();
  while( (millis()-previous) < 5000 )
  {
    // check available data in RX buffer
    if( xbeeDM.available() > 0 ) 
    {
      // parse information
      xbeeDM.treatData(); 

      // check RX flag after 'treatData'
      if( !xbeeDM.error_RX ) 
      {
        // check available packets
        while( xbeeDM.pos>0 )
        {
          /*** Available info in 'xbeeDM.packet_finished' structure ***/
          USB.println(F("\n---------- New Packet Received ---------------"));          


          // 1.Print Source's MAC address
          USB.print(F("MAC Address Source: "));          
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSH[0]); 
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSH[1]); 
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSH[2]); 
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSH[3]); 
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSL[0]); 
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSL[1]); 
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSL[2]); 
          USB.printHex(xbeeDM.packet_finished[xbeeDM.pos-1]->macSL[3]);  
          USB.println("");


          // 2.Print Origin's sending mode: 0=unicast; 1=broadcast;     
          USB.print(F("UNICAST(0) or BROADCAST(1): "));   
          USB.println(xbeeDM.packet_finished[xbeeDM.pos-1]->mode,HEX);

          // 3.Print data field
          USB.print(F("Data: "));                    
          for(int i=0;i<xbeeDM.packet_finished[xbeeDM.pos-1]->data_length;i++)          
          {
            USB.print(xbeeDM.packet_finished[xbeeDM.pos-1]->data[i],BYTE);          
          }
          USB.println("");
          
        
          // free variable 
          free(xbeeDM.packet_finished[xbeeDM.pos-1]);
          xbeeDM.packet_finished[xbeeDM.pos-1]=NULL;

          // decrement packet counter
          xbeeDM.pos--;       

          USB.println(F("----------------------------------------------"));
        }
        previous=millis();
      }
    }
  }/* end while */

}



